using System;

using System.Drawing;
using System.Windows.Forms;
using SPTraceView.Model;

namespace SPTraceView
{
    public partial class frmOptions : Form
    {
        internal readonly TraceSettings Settings;

        internal frmOptions(Icon icon, TraceSettings settings)
        {
            InitializeComponent();

            this.Icon = icon;

            Settings = settings;
            LoadSettings();
        }

        private void LoadSettings()
        {
            cmbIncludeProcessFilter.Text = Settings.ProcessIncludeFilter;
            cbxNotAllProcesses.Checked = !Settings.IncludeAllProcesses;
            cmbIncludeMessages.Text = Settings.MessageIncludeFilter;
            cmbExcludeMessages.Text = Settings.MessageExcludeFilter;
            cmbIncludeCategories.Text = Settings.CategoryIncludeFilter;
            cmbExcludeCategories.Text = Settings.CategoryExcludeFilter;
            cmbIncludeProducts.Text = Settings.ProductIncludeFilter;
            cmbExcludeProducts.Text = Settings.ProductExcludeFilter;

            LoadHistory(Settings.Last10IncludeProcesses, cmbIncludeProcessFilter);
            LoadHistory(Settings.Last10IncludeMessages, cmbIncludeMessages);
            LoadHistory(Settings.Last10ExcludeMessages, cmbExcludeMessages);
            LoadHistory(Settings.Last10IncludeCategories, cmbIncludeCategories);
            LoadHistory(Settings.Last10ExcludeCategories, cmbExcludeCategories);
            LoadHistory(Settings.Last10IncludeProducts, cmbIncludeProducts);
            LoadHistory(Settings.Last10ExcludeProducts, cmbExcludeProducts);
        }

        private void LoadHistory(HistoryList list, ComboBox cmb)
        {
            cmb.Items.Clear();

            foreach (string item in list.Items)
            {
                cmb.Items.Add(item);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //TODO: Add special validation such as 
            //     
            //     - Check for * inclusion, exclusion tokens and warn
            //     - Check for something that doesn't look like a process name and warn

            Settings.ProcessIncludeFilter = cmbIncludeProcessFilter.Text;
            Settings.IncludeAllProcesses = !cbxNotAllProcesses.Checked;
            Settings.MessageIncludeFilter = cmbIncludeMessages.Text;
            Settings.MessageExcludeFilter = cmbExcludeMessages.Text;
            Settings.CategoryIncludeFilter = cmbIncludeCategories.Text;
            Settings.CategoryExcludeFilter = cmbExcludeCategories.Text;
            Settings.ProductIncludeFilter = cmbIncludeProducts.Text;
            Settings.ProductExcludeFilter = cmbExcludeProducts.Text;

            DialogResult = DialogResult.OK;
            Close();
        }

        private void cbxNotAllProcesses_CheckedChanged(object sender, EventArgs e)
        {
            pnlProcessFilter.Visible = cbxNotAllProcesses.Checked;
        }

        private void frmOptions_Load(object sender, EventArgs e)
        {
            btnCancel.Select();
            btnCancel.Focus();
        }

        private void btnFarmSettings_Click(object sender, EventArgs e)
        {
            frmFarmSettings frm = new frmFarmSettings(this.Icon, Settings.FarmRole);
            frm.StartPosition = FormStartPosition.CenterParent;
            if (frm.ShowDialog(this) == DialogResult.OK)
            {
                // TODO: Save settings
            }
        }
    }
}